<?php
/**
 * +----------------------------------------------------------------------
 * | 九月科技-ztuc.cn
 * +----------------------------------------------------------------------
 *                      .::::.
 *                    .::::::::.            | AUTHOR: siyu
 *                    :::::::::::           | EMAIL: ztucke@ztuc.cn
 *                 ..:::::::::::'           | DATETIME: 2020/01/31
 *             '::::::::::::'
 *                .::::::::::
 *           '::::::::::::::..
 *                ..::::::::::::.
 *              ``::::::::::::::::
 *               ::::``:::::::::'        .:::.
 *              ::::'   ':::::'       .::::::::.
 *            .::::'      ::::     .:::::::'::::.
 *           .:::'       :::::  .:::::::::' ':::::.
 *          .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *     ...:::           ::::::::::::'              ``::.
 *   ```` ':.          ':::::::::'                  ::::..
 *                      '.:::::'                    ':'````..
 * +----------------------------------------------------------------------
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/4
 * Time: 12:13
 */

namespace app\lnadmin\controller;

use think\facade\Filesystem;

/**
 * 图片上传
 * Class Upload
 * @package app\controller\common
 */
class Upload
{

    const TYPE_BANNER     = 1;                 // 轮播图片
    const BANNER_PATH     = 'banner';          // 路径

    const TYPE_ADMIN      = 2;                 //管理员头像图片
    const ADMIN_PATH      = 'lnadmin';         // 路径

    const TYPE_USER       = 3;                 //会员头像图片
    const USER_PATH       = 'user';            // 路径

    const TYPE_ARTICLE    = 4;                 //文章图片
    const ARTICLE_PATH    = 'article';         // 路径


    const TYPE_SYSTEM     = 5;                 //logo图片
    const SYSTEM_PATH     = 'logo';            // 路径

    const TYPE_FILEUPLOAD = 6;                 //文件压缩包
    const FILEUPLOAD_PATH = 'fileupload';      // 路径

    /**
     * 创建文件目录
     * @param string $path
     * @author: MK
     * @Time: 2020/4/5 12:33
     */
     public static function makeDir(string $path)
    {
        $list = explode('/', $path);

        if (!empty($list)) {
            $temp = '';
            foreach ($list as $k => $v) {
                $temp .= $v . '/';
                if (!file_exists($temp)) {
                    @mkdir($temp, 0777);
                    chmod($temp, 0777);
                }
            }
        }
    }

   
    /**
     * 图片上传
     * @param $path
     * @return array
     * @author: MK
     * @Time: 2020/4/5 12:33
     */
   
     public static function uploadImage()
    {   
        try {
         $param = request()->param(); 
             switch ($param['model']) {
                    case '1': $path = self::BANNER_PATH; break;
                    case '2': $path = self::ADMIN_PATH; break;
                    case '3': $path = self::USER_PATH; break;
                    case '4': $path = self::ARTICLE_PATH; break;
                    case '5': $path = self::SYSTEM_PATH; break;
                    default:  $path = self::FILEUPLOAD_PATH;
                }

        $result = [
            'code' => -1,
            'msg' => __('Upload failed'),
        ];
      
        // 上传图片 
        $file            = request()->file('file');
        $salt            = substr(md5(time()), -10);    //密码盐
        $fileInfo        = pathinfo($file);
        $extension       = strtolower($file->getOriginalExtension());
        $filePath        = $fileInfo['dirname'] . '/' . $fileInfo['basename'];
        $getOriginalMime = $file->getOriginalMime();
        $getSize         = $file->getSize();
           
        $file = new \think\File($filePath);
        $savePath = root_path() . 'public/uploads/' . $path . '/' . date('Y'). '/' . date('m'). '/' . date('d') . '/';
        $saveName = '/uploads/' . $path . '/' . date('Y'). '/' . date('m'). '/' . date('d') . '/' . $salt . '.' . $extension;
        $file->move($savePath, $salt . '.' . $extension);
        
       if ($extension) {
            $result['code']     = 1;
            $result['msg']      = '上传成功' . $salt . '.' . $extension;
            $result['url']      = $saveName;
            $result['filesize'] = $getSize;
            $result['mine']     = $getOriginalMime;
        }
        add_admin_log('uploadImage', '图片');// 记录后台行为日志
       return $result;     
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => $e->getMessage()];
        }
    }  
     public static function uploadFile()
    {   
        try {
            
         $path = self::FILEUPLOAD_PATH;

        $result = [
            'code' => -1,
            'msg'  => __('Upload failed'),
        ];
      
        // 上传图片 
        $file            = request()->file('file');
        $fileInfo        = pathinfo($file);
        $filePath        = $fileInfo['dirname'] . '/' . $fileInfo['basename'];
        $originalName    = $_FILES['file']['name'];//原文件名
        $getOriginalMime = $file->getOriginalMime();//文件类型
        $getSize         = $file->getSize();//文件大小
           
        $file = new \think\File($filePath);
        $savePath = root_path() . 'public/uploads/' . $path . '/' . date('Y'). '/' . date('m'). '/' . date('d') . '/';
        $saveName = '/uploads/' . $path . '/' . date('Y'). '/' . date('m'). '/' . date('d') . '/' . $originalName;
       
        $file->move($savePath, $originalName);//文件移动路径
        
       if ($originalName) {
            $result['code']     = 1;
            $result['msg']      = '上传成功' . $originalName;
            $result['url']      = $saveName;
            $result['filesize'] = $getSize;
            $result['mine']     = $getOriginalMime;
        }
        add_admin_log('uploadFile', '上传文件');// 记录后台行为日志
       return $result;     
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => $e->getMessage()];
        }
    }
    /**
     * 获取图片路径
     * @param $url
     * @return string
     * @author: MK
     * @Time: 2020/4/5 12:33
     */
    public static function image($url)
    {
        $url = $url ?? '/static/libs/images/no_image'.mt_rand(1,10).'.jpg'; // 默认图片
        return request()->domain() . $url;
    }
}
